package com.ruicar.afs.cloud.basic.grace.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.api.dto.CasePrototypeDto;
import com.ruicar.afs.cloud.basic.api.dto.ProductPrototypeDto;
import com.ruicar.afs.cloud.basic.api.fegin.casefeign.CaseServiceFeign;
import com.ruicar.afs.cloud.basic.api.product.feign.ProductServiceFeign;
import com.ruicar.afs.cloud.basic.common.entity.BasicCompanyGraceConfig;
import com.ruicar.afs.cloud.basic.common.entity.BasicProductGraceConfig;
import com.ruicar.afs.cloud.basic.common.entity.BasicSpecialGraceConfig;
import com.ruicar.afs.cloud.basic.common.service.BasicCompanyGraceConfigService;
import com.ruicar.afs.cloud.basic.common.service.BasicProductGraceConfigService;
import com.ruicar.afs.cloud.basic.common.service.BasicSpecialGraceConfigService;
import com.ruicar.afs.cloud.basic.grace.condition.GraceCondition;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ValidOrInvalidEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysAddressParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author chuke
 * @create 2020/7/9 10:49
 */
@Slf4j
@RestController
@RequestMapping("/graceInfo")
@AllArgsConstructor
public class GraceController {
    private final BasicCompanyGraceConfigService companyGraceConfigService;
    private final BasicProductGraceConfigService productGraceConfigService;
    private final BasicSpecialGraceConfigService specialGraceConfigService;
    private final ProductServiceFeign productServiceFeign;
    private final CaseServiceFeign caseServiceFeign;


    @ApiOperation("查询公司宽限配置")
    @PostMapping(value = "/findCompanyGrace")
    public IResponse<BasicCompanyGraceConfig> findCompanyGrace() {
        BasicCompanyGraceConfig graceConfigs = companyGraceConfigService.getOne(Wrappers.<BasicCompanyGraceConfig>lambdaQuery());
        return new IResponse().setData(graceConfigs);
    }

    @ApiOperation("保存公司宽限配置")
    @PostMapping(value = "/saveCompanyGrace")
    public IResponse saveCompanyGrace(@RequestBody BasicCompanyGraceConfig companyGrace) {
        if(EmptyUtils.isEmpty(companyGrace.getId())){
            companyGrace.setSatatus(ValidOrInvalidEnum.valid);
            companyGraceConfigService.save(companyGrace);
        }else{
            companyGraceConfigService.updateById(companyGrace);
        }
        return new IResponse().setData(companyGrace);
    }


    @ApiOperation("查询产品宽限配置")
    @PostMapping(value = "/findProductGrace")
    public IResponse<IPage<List<BasicProductGraceConfig>>> findProductGrace(@RequestBody GraceCondition condition) {

        IPage<List<BasicProductGraceConfig>> graceConfigs = productGraceConfigService.page(new Page(condition.getPageNumber()
                , condition.getPageSize()), Wrappers.<BasicProductGraceConfig>lambdaQuery()
                .eq(BasicProductGraceConfig::getStatus, condition.getStatus()));
        return new IResponse().setData(graceConfigs);
    }


    @ApiOperation("查询地区宽限配置")
    @PostMapping(value = "/findAreaGrace")
    public IResponse<IPage<List<BasicSpecialGraceConfig>>> findAreaGrace(@RequestBody GraceCondition condition) {
        //查询处于激活状态的地区记录
        IPage<List<BasicSpecialGraceConfig>> graceConfigs = specialGraceConfigService.page(new Page(condition.getPageNumber()
                , condition.getPageSize()), Wrappers
                .<BasicSpecialGraceConfig>lambdaQuery()
                .eq(BasicSpecialGraceConfig::getStatus, condition.getStatus()));
        return new IResponse().setData(graceConfigs);
    }


    @ApiOperation("保存地区宽限配置")
    @PostMapping(value = "/saveAreaGrace")
    public IResponse saveAreaGrace(@RequestBody BasicSpecialGraceConfig config) {
        //根据产品编号新增或者更新记录
        if (config.getId() != null) {
            specialGraceConfigService.updateById(config);
            return new IResponse().setMsg("更新成功");
        } else {
            if (specialGraceConfigService.getOne(Wrappers.<BasicSpecialGraceConfig>lambdaQuery()
                    .eq(BasicSpecialGraceConfig::getLicensedAreaCode, config.getLicensedAreaCode())) != null) {
                return IResponse.fail("该地区已存在请勿重复保存");
            }
            specialGraceConfigService.save(config);
            return new IResponse().setMsg("保存成功");
        }
    }


    @ApiOperation("保存产品宽限配置")
    @PostMapping(value = "/saveProductGrace")
    public IResponse saveProductGrace(@RequestBody BasicProductGraceConfig config) {
        //根据地区编号新增或者更新记录
        if (config.getId() != null) {
            productGraceConfigService.updateById(config);
            return new IResponse().setMsg("更新成功");
        } else {
            if (productGraceConfigService.getOne(Wrappers.<BasicProductGraceConfig>lambdaQuery()
                    .eq(BasicProductGraceConfig::getProductNo, config.getProductNo())) != null) {
                return IResponse.fail("该产品已存在请勿重复保存");
            }
            productGraceConfigService.save(config);
            return new IResponse().setMsg("保存成功");
        }
    }

    @ApiOperation("查询产品列表")
    @PostMapping(value = "/findProductList")
    public IResponse findProductList() {
        QueryCondition<ProductPrototypeDto> planCondition = new QueryCondition<ProductPrototypeDto>();
        ProductPrototypeDto dto = new ProductPrototypeDto();
        //不分页，直接给个最大值
        planCondition.setPageNumber(1L);
        planCondition.setPageSize(9999999999999999L);
        planCondition.setCondition(dto);
        IResponse<List<ProductPrototypeDto>> list = productServiceFeign.queryProductProposalList(planCondition);
        return new IResponse().setData(list.getData());
    }

    @ApiOperation("删除产品列表")
    @PostMapping(value = "/delProductInfo")
    public IResponse delProductInfo(@RequestBody BasicProductGraceConfig config) {
        boolean remove = productGraceConfigService.removeById(config.getId());
        if (remove) {
            return new IResponse().setMsg("删除成功");
        } else {
            return new IResponse().setMsg("删除失败");

        }
    }

    @ApiOperation("删除地区列表")
    @PostMapping(value = "/delAreaInfo")
    public IResponse delAreaInfo(@RequestBody BasicSpecialGraceConfig config) {
        boolean remove = specialGraceConfigService.removeById(config.getId());
        if (remove) {
            return new IResponse().setMsg("删除成功");
        } else {
            return new IResponse().setMsg("删除失败");

        }
    }

    @ApiOperation("获取地区名称列表")
    @PostMapping(value = "/findAddressList")
    public IResponse findAddressList(@RequestBody CasePrototypeDto dto) {
        IResponse<List<TsysAddressParam>> cityInfos = caseServiceFeign.getAddressList(dto);
        return new IResponse().setData(cityInfos.getData());
    }
}
